<?php
namespace app\erp\controller;

use app\erp\model\Depotdb;
use app\erp\model\Purchasedb;
use think\Db;

class Depot extends Base
{

    protected $db;

    protected $cols = array(
        array(
            'field' => 'depot_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'depot_name',
            'width' => 200,
            'title' => '库房名称'
        ),
        array(
            'field' => 'is_third',
            'width' => 100,
            'title' => '第三方库房',
            'templet' => '#is_thirdTpl'
        ),
        array(
            'field' => 'depot_addr',
            'width' => 300,
            'title' => '库房地址'
        ),
        array(
            'field' => 'depot_contacts',
            'width' => 120,
            'title' => '库房联系人'
        ),
        array(
            'field' => 'depot_phone',
            'width' => 120,
            'title' => '联系电话'
        ),
        array(
            'fixed' => 'right',
            'width' => 160,
            'toolbar' => '#depot_tool',
            'title' => '操作'
        )
    );

    protected $cols_purchase = array(
        array(
            'field' => 'purchase_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'order_no',
            'width' => 200,
            'title' => '订单编号'
        ),
        array(
            'field' => 'goods_sku',
            'width' => 200,
            'title' => '商品编号'
        ),
        array(
            'field' => 'goods_name',
            'width' => 300,
            'title' => '商品名称'
        ),
        array(
            'field' => 'create_time',
            'width' => 160,
            'title' => '采购时间'
        ),
        array(
            'field' => 'depot_name',
            'width' => 160,
            'title' => '入库库房'
        ),
        array(
            'field' => 'is_third',
            'width' => 100,
            'title' => '第三方库房',
            'templet' => '#is_thirdTpl'
        ),
        array(
            'field' => 'purchase_num',
            'width' => 80,
            'title' => '数量'
        ),
        array(
            'field' => 'in_depot',
            'width' => 80,
            'title' => '入库',
            'templet' => '#in_depotTpl'
        ),
        array(
            'field' => 'depot_time',
            'width' => 120,
            'title' => '入库时间'
        ),
        array(
            'field' => 'is_posting',
            'width' => 80,
            'title' => '过账',
            'templet' => '#is_postingTpl'
        ),
        array(
            'field' => 'posting_time',
            'width' => 120,
            'title' => '过账时间'
        ),
        array(
            'field' => 'admin_name',
            'width' => 120,
            'title' => '采购账号'
        ),
        array(
            'fixed' => 'right',
            'width' => 100,
            'toolbar' => '#ingoods_tool',
            'title' => '操作'
        )
    );

    protected $cols_out = array(
        array(
            'field' => 'purchase_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'order_no',
            'width' => 200,
            'title' => '订单编号'
        ),
        array(
            'field' => 'goods_sku',
            'width' => 200,
            'title' => '商品编号'
        ),
        array(
            'field' => 'goods_name',
            'width' => 300,
            'title' => '商品名称'
        ),
        array(
            'field' => 'depot_name',
            'width' => 160,
            'title' => '入库库房'
        ),
        array(
            'field' => 'purchase_num',
            'width' => 80,
            'title' => '数量'
        ),
        array(
            'field' => 'depot_time',
            'width' => 120,
            'title' => '入库时间'
        ),
        array(
            'fixed' => 'right',
            'width' => 100,
            'toolbar' => '#outgoods_tool',
            'title' => '操作'
        )
    );

    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        $this->db = new Depotdb();
        $this->db->param = $this->post;
    }
    
    // 入库
    public function ingoods()
    {
        $this->db->param['_o'] = 'depot_id';
        $this->db->param['_b'] = 'desc';
        $this->db->param['page'] = 1;
        $this->db->param['limit'] = 50;
        $this->db->param['is_third'] = 2;
        $depot_data = $this->db->ajaxList();
        $this->assign('depot_data', $depot_data);
        $this->assign('cols', json_encode($this->cols_purchase));
        return $this->fetch();
    }
    // 出库
    public function outgoods()
    {
        $this->db->param['_o'] = 'depot_id';
        $this->db->param['_b'] = 'desc';
        $this->db->param['page'] = 1;
        $this->db->param['limit'] = 50;
        $this->db->param['is_third'] = 2;
        $depot_data = $this->db->ajaxList();
        $this->assign('depot_data', $depot_data);
        $this->assign('cols', json_encode($this->cols_out));
        return $this->fetch();
    }

    public function ajaxIngoodsList()
    {
        $this->db = new Purchasedb();
        $this->db->param = $this->post;
        $this->db->param['no_need_cart_price'] = 'yes';
        $data = $this->db->ajaxSelect();
        $this->layuiReturn($data['data'], $data['count']);
    }

    public function ajaxOutgoodsList()
    {
        $this->db = new Purchasedb();
        if (! isset($this->post['out_depot'])) {
            $this->post['out_depot'] = 2;
        }
        $this->db->param = $this->post;
        $this->db->param['is_third'] = 2;
        $this->db->param['in_depot'] = 1;
        $this->db->param['no_need_cart_price'] = 'yes';
        $data = $this->db->ajaxSelect();
        $this->layuiReturn($data['data'], $data['count']);
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function tplForm()
    {
        $depot_id = 0;
        $data = array();
        if (isset($this->post['depot_id'])) {
            $depot_id = $this->post['depot_id'];
            $data = $this->db->ajaxFind();
        }
        $this->assign('depot_id', $depot_id);
        $this->assign('data', json_encode($data));
        return $this->fetch();
    }

    public function ajaxList()
    {
        $return = $this->db->ajaxSelect();
        //$rand = 'WL' . base_convert(uniqid(), 16, 10);
        $this->layuiReturn($return['data']);
    }
    public function createNo(){
        $rand = 'WL' . base_convert(uniqid(), 16, 10);
        $this->layuiReturn(array(), $rand);
    }

    public function ajaxIndex()
    {
        $return = $this->db->ajaxSelect();
        $this->layuiReturn($return['data'], $return['count']);
    }
    // 过账入库
    public function depotPostingSave()
    {
        $this->db = new Purchasedb();
        $this->db->param = $this->post;
        if (is_array($this->post['purchase_id'])) {
            $return = $this->db->ajaxSaveAll();
            $this->layuiReturn($return);
        } else {
            $return = $this->db->ajaxSave();
            if ($return != 'success') {
                $this->layuiReturn($return, 0, $return, - 1);
            } else {
                $this->layuiReturn('保存成功');
            }
        }
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $this->db->param = $this->post;
        $return = $this->db->ajaxSave();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, - 1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }

    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $this->db->param = $this->post;
        $this->db->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }
}